package my.dao.daoImpl;

import my.dao.CommonDao;
import my.enums.Role;
import my.utils.ObjUtils;
import my.vo.voImpl.User;

import java.util.List;

/**
 * @author 星环
 * @date 2022/11/13 13:42:40
 * @description
 */
public class UserDao extends CommonDao<User> {
    private static final UserDao userDao = new UserDao();

    private UserDao(){
        super(new User());
    }


    public static UserDao getInstance(){
        return userDao;
    }


    /**
     * 注册前要先检查是否重名，这是用于注册前调用的
     *
     * @param user 要添加的用户信息
     */
    public User addNewVoWithNameCheck(User user) {
        List<User> users = srchBy(user1 -> user1.getUsername().equals(user.getUsername()));
        if(users.size() > 0) {
            return null;
        }
        addNewVo(user);
        return user;
    }

    public User deleteById(int id){
        List<User> users = deleteBy(user -> user.getId() == id);
        return (User) ObjUtils.getFirstOrNull(users);
    }

    /**
     *
     * @param name
     * @return 被删除的用户
     */
    public User deleteByName(String name){
        List<User> users = deleteBy(user -> user.getUsername().equals(name));
        return (User) ObjUtils.getFirstOrNull(users);
    }

    public User updateById(User user){
        for (User user1 : list) {
            if (user1.getId() == user.getId()) {
                if (user.getUsername() != null) {
                    if (srchBy(user2 -> user2.getUsername().equals(user.getUsername())).size() != 0) {
                        return null;
                    }
                    user1.setUsername(user.getUsername());
                }
                if (user.getPassword() != null) {
                    user1.setPassword(user.getPassword());
                }
                if (user.getRole() != null) {
                    user1.setRole(user.getRole());
                }
                return (User) user1.clone();
            }
        }
        return null;
    }

    private User getByNameIgnoreId(String username, int id) {
        List<User> users = srchBy(user -> user.getId() != id && user.getUsername().equals(username));
        return (User) ObjUtils.getFirstOrNull(users);
    }

    public List<User> getAll(){
        return srchBy(user -> true);
    }

    public User getByName(String name){
        List<User> users = srchBy(user -> user.getUsername().equals(name));
        return (User) ObjUtils.getFirstOrNull(users);
    }

    public User getById(int id){
        List<User> users = srchBy(user -> user.getId() == id);
        return (User) ObjUtils.getFirstOrNull(users);
    }

    /**
     * 登录方法
     * @param username 用户名
     * @param password 密码
     * @return 如果登录成功，返回他的信息，用于后续使用，否则返回null
     */
    public User login(String username, String password) {
        List<User> users = srchBy(user -> user.getUsername().equals(username) && user.getPassword().equals(password));
        return (User) ObjUtils.getFirstOrNull(users);
    }

    /**
     * 注册
     * @param username
     * @param password
     * @return 注册成功，返回用户，重名返回null
     */
    public User register(String username, String password) {
        User user = new User(username, password);
        user.setRole(Role.READER);
        return addNewVoWithNameCheck(user);
    }
}
